package background;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import shared.*;

import background.Benchmark1;

public class SearchCmd1 {

	public static void main (String[] args) throws IOException {
		String name;

		// Check that a filename has been given as argument
		if (args.length < 1) {
			System.out.println("Usage: java SearchCmd <datafile>");
			System.exit(1);
		}

		// Read the file and create the linked list
		HTMLlist l = Searcher1.readHtmlList (args[0]);

		// if output file defined in arguments - run benchmark
		if (args.length > 1){ 

			// create new benchmark test and run it
			Benchmark1 myBenchmark = new Benchmark1();
			myBenchmark.runBenchmark( l, args[0], args[1]);
		}

		// Ask for a word to search
		BufferedReader inuser =
				new BufferedReader (new InputStreamReader (System.in));

		System.out.println ("Hit return to exit.");
		boolean quit = false;
		while (!quit) {
			System.out.print ("Search for: ");
			name = inuser.readLine(); // Read a line from the terminal
			if (name == null || name.length() == 0) {
				quit = true;
			} else if (Searcher1.exists (l, name)) {
				System.out.println ("The word \""+name+"\" has been found.");
			} else {
				System.out.println ("The word \""+name+"\" has NOT been found.");
			}
		}
	}
}